'use strict';
var cmsControllers = angular.module('cmsControllers');

cmsControllers
    .controller('CmsGalleryCtrl', ['$rootScope', '$scope', '$routeParams', 'uploadService',
            'Subscription', 'GalleryOpt', 'Gallery', 'Pictures', 'Picture',
        function ($rootScope, $scope, $routeParams, uploadService, Subscription, GalleryOpt, Gallery, Pictures, Picture) {
            $scope.showPictureList = function() {
                $scope.galleryContent = 'views/component/gallery/picture-list.html';
                var json = GalleryOpt.opt({action: 'queryGalleryBySubId', subId: $rootScope.CurrentSub.id},function() {
                    $scope.gallery = json.data.gallery;
                    $scope.pictures = json.data.pictures;
                });
            }
            $scope.showPictureList();

            $scope.createPicture = function() {
                uploadService.maxNumb = 1;
                uploadService.files = [];
                $scope.galleryContent = 'views/component/gallery/picture-edit.html';
                $scope.picture = {galleryId: $scope.gallery.id};
            };
            $scope.editPicture = function(picture) {
                uploadService.maxNumb = 1;
                uploadService.files = [];
                $scope.galleryContent = 'views/component/gallery/picture-edit.html';
                $scope.picture = picture;

                uploadService.files.push({url: picture.url, thumbnailUrl: uploadService.imageUrl(picture.url, 'thumbnail')});
                $scope.files = uploadService.files;
            };
            $scope.savePicture = function() {
                $scope.picture.url = uploadService.files[0].url
                new Picture($scope.picture).$save().then(function() {
                    $scope.showPictureList();
                });
            };
            $scope.deletePicture = function(picture) {
                new Picture(picture).$delete().then(function() {
                    $scope.showPictureList();
                });
            };
            $scope.noImage = function() {
                if (uploadService.files.length < 1) {
                    return true;
                } else {
                    return false;
                }
            };
    }]);




